package com.ytktt.study.week5.task1.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Hello Service的 Cglib动态代理
 *
 * @author ytk
 */
public class HelloServiceCglibProxy implements MethodInterceptor {

    private Object target;

    public Object bind(Object target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("do sth before...");
        Object result = methodProxy.invokeSuper(o, objects);
        System.out.println("do sth after...");
        return result;
    }
}
